package com.woniuxy.util;

import cn.hutool.core.date.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.concurrent.TimeUnit;

/**
 * @BelongsProject: charging-management-platform
 * @BelongsPackage: com.woniuxy.util
 * @Author: Quan
 * @Date: 2025/3/31 16:07
 */
@Component
public class CodeUtil {
	@Autowired
	private RedisTemplate<String, String> redisTemplate;
	public String generateRegularCode(String prefix) {
		String datePart = new SimpleDateFormat("yyyyMMdd").format(new DateTime());
		String key = "CACHE:CHARGINGPLATFORM:CODEGENERATE" + prefix + ":" + datePart;

		// 使用Redis原子递增
		Long sequence = redisTemplate.opsForValue().increment(key, 1);

		// 设置过期时间(可选)
		redisTemplate.expire(key, 1, TimeUnit.DAYS);

		return prefix + "-" + datePart + "-" + String.format("%04d", sequence);
	}
}
